package com.dzu.easy;

/**
 * @author ZhaoDong
 * @date 2022/9/29 10:10
 * @description 557. 反转字符串中的单词 III
 */
public class Test557 {
    public static void main(String[] args) {
        System.out.println(reverseWords2("Let's take LeetCode contest"));

    }

    // 4ms
    public static String reverseWords2(String s) {
        String[] split = s.split(" ");
        for (int i = 0; i < split.length; i++) {
            split[i] = new StringBuffer(split[i]).reverse().toString();
        }
        return String.join(" ", split);

    }

    // 20ms
    public static String reverseWords(String s) {
        String rest = "";
        String[] split = s.split(" ");
        for (int i = 0; i < split.length; i++) {
            String str = split[i];
            char[] chars = str.toCharArray();
            int start = 0;
            int end = chars.length - 1;
            while (start < end) {
                char endChar = chars[end];
                chars[end] = chars[start];
                chars[start] = endChar;
                start++;
                end--;
            }
            rest += String.valueOf(chars) + " ";
        }
        return rest.trim();

    }
}
